package com.hngy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hngy.common.CustomException;
import com.hngy.dao.SetmealDao;
import com.hngy.domain.Category;
import com.hngy.domain.Setmeal;
import com.hngy.domain.SetmealDish;
import com.hngy.dto.SetmealDto;
import com.hngy.service.CategoryService;
import com.hngy.service.DishService;
import com.hngy.service.SetmealDishService;
import com.hngy.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LiKe
 * @date 2021/12/26 16:17
 */
@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @Override
    public void add(SetmealDto setmealDto) {
        //添加套餐
        this.save(setmealDto);
        //添加套餐才的菜品
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();

        setmealDishList=setmealDishList.stream().map((item)->{

            item.setSetmealId(setmealDto.getId());

            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishList);

    }

    @Override
    public IPage<SetmealDto> myPage(int page, int pageSize, String name) {
        //分页查询
        IPage<Setmeal> pageInfo = new Page<>(page,pageSize);

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        queryWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);

        this.page(pageInfo,queryWrapper);

        List<Setmeal> setmealList = pageInfo.getRecords();

        List<SetmealDto> setmealDtoList = setmealList.stream().map((item)->{
            //查询套餐分类
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);

            SetmealDto setmealDto = new SetmealDto();
            //copy
            BeanUtils.copyProperties(item,setmealDto);

            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());

        IPage<SetmealDto> page1 = new Page<>();
        BeanUtils.copyProperties(pageInfo,page1);
        page1.setRecords(setmealDtoList);

        return page1;
    }

    @Override
    public void delByIds(List<Long> ids) {
        //判断是否停售
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        wrapper.eq(Setmeal::getStatus,1);
        int count = this.count(wrapper);
        if (count>0){
            throw new CustomException("套餐未停售！删除失败！");
        }

        //删除套餐
        this.removeByIds(ids);
        //删除套餐下的菜品

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.in(SetmealDish::getSetmealId,ids);

        setmealDishService.remove(queryWrapper);


    }

    @Override
    public void updateStatus(int status, List<Long> ids) {
        //根据id修改状态
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Setmeal::getId,ids);
        updateWrapper.set(Setmeal::getStatus,status);

        this.update(updateWrapper);

    }
}
